package com.yanqu.road.dao.impl.activity.biaoju;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.biaoju.config.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BiaoJuConfigImpl extends TempDao {
    private static String getBiaoJuCityConfigSql = "select * from `t_s_activity_biaoju_city` where `activityId`=?";
    public Map<Integer, BiaoJuCityConfig> getBiaoJuCityConfigMap(int activityId) {
        Map<Integer, BiaoJuCityConfig>  dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getBiaoJuCityConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BiaoJuCityConfig biaoJuCityConfig = new BiaoJuCityConfig(rs);
                dataMap.put(biaoJuCityConfig.getId(), biaoJuCityConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private static String getBiaoJuDartConfigSql = "select * from `t_s_activity_biaoju_dart` where `activityId`=?";
    public Map<Integer, BiaoJuDartConfig> getBiaoJuDartConfigMap(int activityId) {
        Map<Integer, BiaoJuDartConfig> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getBiaoJuDartConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BiaoJuDartConfig data = new BiaoJuDartConfig(rs);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private static String getBiaoJuEventConfigSql = "select * from `t_s_activity_biaoju_event` where `activityId`=?";
    public List<BiaoJuEventConfig> getBiaoJuEventConfigList(int activityId) {
        List<BiaoJuEventConfig> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getBiaoJuEventConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BiaoJuEventConfig dataObject = new BiaoJuEventConfig(rs);
                dataList.add(dataObject);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    private static String getBiaoJuTitleConfigSql = "select * from `t_s_activity_biaoju_title` where `activityId`=?";
    public Map<Integer, BiaoJuTitleConfig> getBiaoJuTitleConfigMap(int activityId) {
        Map<Integer, BiaoJuTitleConfig> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getBiaoJuTitleConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BiaoJuTitleConfig data = new BiaoJuTitleConfig(rs);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private static String getBiaoJuCityRankConfigSql = "select * from `t_s_activity_biaoju_place_rank` where `activityId`=?";
    public List<BiaoJuCityRankConfig> getBiaoJuCityRankConfigList(int activityId) {
        List<BiaoJuCityRankConfig> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getBiaoJuCityRankConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BiaoJuCityRankConfig dataObject = new BiaoJuCityRankConfig(rs);
                dataList.add(dataObject);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    private static String getBiaoJuLevelConfigSql = "select * from `t_s_activity_biaoju_level` where `activityId`=?";
    public Map<Integer, BiaoJuLevelConfig> getBiaoJuLevelConfigMap(int activityId) {
        Map<Integer, BiaoJuLevelConfig> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getBiaoJuLevelConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BiaoJuLevelConfig data = new BiaoJuLevelConfig(rs);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
